package 项目版本管理实践;

import java.util.Random;
import java.util.Scanner;
//同一区域修改
public class GeneratePassword {
//媛媛修改
	public static void main(String[] args) {
		for (int i = 0; i < 20; i++) {
			//火星新文件
			System.out.println("========================");
			System.out.println("欢迎使用密码管理系统");
			System.out.println("========================");
			System.out.println("请选择操作：");
			System.out.println("1.加密");
			System.out.println("2.解密");
			System.out.println("3.判断密码强度");
			System.out.println("4.密码生成");
			System.out.println("5.退出");
			System.out.println();

			Scanner sc = new Scanner(System.in);
			code c = new code();
			String str = new String();
			System.out.print("请输入一个字符串：");
			str = sc.next();

			System.out.print("请输入要执行的操作：");
			int number = sc.nextInt();

			int y = 3;
			c.setKey(y);

			switch (number) {
			case 1:
				System.out.println("字符串加密后的结果为：" + c.encrypt(str));
				break;
			case 2:
				System.out.println("字符串解密后的结果为：" + c.decode(str));
				break;
			case 3:
				System.out.println("判断密码强度的结果为:" + c.DetermineStrength(str));
				break;
			case 4:
				System.out.println("密码生成:");
				c.generateNumber();
				System.out.println();
				break;
			default:
				System.out.println("已成功退出");
				return;
			}
		}
	}
}

class code {
	private int key;

	public code() {
		key = 0;
	}

	public void setKey(int y) {
		key = y;
	}

	public String encrypt(String s) {
		String r = "";
		for (int i = 0; i < s.length(); i++) {
			r += (char) (s.charAt(i) + 3);
		}
		return r;
	}

	public String decode(String s) {
		String r = "";
		for (int i = 0; i < s.length(); i++) {
			r += (char) (s.charAt(i) - 3);
		}
		return r;
	}

	public String DetermineStrength(String s) {
		String pattern = "^\\d{8,}$";
		String pattern1 = "^[a-z]{8,}$";
		String pattern2 = "^[A-Z]{8,}$";
		String regex = "^(?=.*[0-9])(?=.*[a-z[A-Z]]).{8,}$";
		String high = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}$";
		if (s.length() < 8 || s.matches(pattern) || s.matches(pattern1) || s.matches(pattern2)) {
			System.out.println(s.matches(pattern));
			return "弱强度";
		} else if (s.matches(regex)) {

			return "中强度";
		} else if (s.matches(high)) {
			return "高强度";
		}
		return "输入格式不正确";
	}

	public void generatePassword(int length) {
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			switch (random.nextInt(3)) {
			case 0:
				char ranLow = (char) ((random.nextInt(26) + 97));
				System.out.print(ranLow);
				break;
			case 1:
				char ranUp = (char) ((random.nextInt(26) + 65));
				System.out.print(ranUp);
				break;
			case 2:
				int ranNum = random.nextInt(10);
				System.out.print(ranNum);
				break;
			}
		}
	}

	public void generateNumber() {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入密码长度：");
		int length = sc.nextInt();
		generatePassword(length);
	}
}
